{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数学和统计方法\n",
    "> 可以通过数组上的一组数学函数对整个数组或某个轴向的数据 进行统计计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.12680766555966114"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# sum、mean以及标准差std等聚合计算(aggregation，通 常叫做约简(reduction))既可以当做数组的实例方法调用，\n",
    "# 也可以当做顶级NumPy函数使用\n",
    "arr = np.random.randn(5, 4) # 正态分布的数据\n",
    "\n",
    "arr.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-2.536153311193223"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 其他如cumsum和cumprod之类的方法则不聚合，而是产生一个由 中间结果组成的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2],\n",
       "       [ 3,  5,  7],\n",
       "       [ 9, 12, 15]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.cumsum(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  0,   0,   0],\n",
       "       [  3,  12,  60],\n",
       "       [  6,  42, 336]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.cumprod(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 基本数组统计方法\n",
    "|方法|说明|\n",
    "|:-|:-|\n",
    "|sum|对数组中全部或某轴向的元素求和，零长度的数组的sum为0|\n",
    "|mean|算术平均数，零长度的数组的mean为NaN|\n",
    "|std var|分别为标准差和方差，自由度可调（默认为n）|\n",
    "|min max|最大值和最小值|\n",
    "|argmin argmax|分别为最大和最小元素的索引|\n",
    "|cumsum|所有元素的累计和|\n",
    "|cumprod|所有元素的累计积|"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 用于布尔型数组的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "54"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.random.randn(100)\n",
    "(arr > 0).sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "bools = np.array([False, False, True, False])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bools.any()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bools.all()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- any用于测试数组中是否存在一个或多个True\n",
    "- all则检查数组中所有值是否都是True\n",
    "- 两个方法也能用于非布尔型数组，所有非0元素将会被当做True"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.11603594, -0.94228666,  1.01127341,  0.76720101, -0.61180954,\n",
       "       -1.64585567,  0.05966358,  1.27602964])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.random.randn(8)\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1.64585567, -0.94228666, -0.61180954,  0.05966358,  0.11603594,\n",
       "        0.76720101,  1.01127341,  1.27602964])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.sort()\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.61576095, -1.53169333, -0.40707572],\n",
       "       [-0.44004617, -0.50962686,  0.10966466],\n",
       "       [-0.33907228,  1.11688127, -0.92513975],\n",
       "       [ 0.56263668, -1.01047434,  0.02348047],\n",
       "       [ 0.39512533, -1.64090881, -1.14289568]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 多维数组可以在任何一个轴向上进行排序，只需将轴编号传给 sort即可\n",
    "arr = np.random.randn(5, 3)\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1.53169333, -0.40707572,  0.61576095],\n",
       "       [-0.50962686, -0.44004617,  0.10966466],\n",
       "       [-0.92513975, -0.33907228,  1.11688127],\n",
       "       [-1.01047434,  0.02348047,  0.56263668],\n",
       "       [-1.64090881, -1.14289568,  0.39512533]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.sort(1)\n",
    "arr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 顶级方法np.sort返回的是数组的已排序副本，而就地排序则会修 改数组本身。计算数组分位数最简单的办法是对其进行排序，然后选 取特定位置的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1.6749791543479473"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "large_arr = np.random.randn(1000)\n",
    "large_arr.sort()\n",
    "large_arr[int(0.05 * len(large_arr))] # 5%分位数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 唯一化以及其他的集合逻辑\n",
    "> NumPy提供了一些针对一维ndarray的基本集合运算。最常用的 可能要数np.unique了，它用于找出数组中的唯一值并返回已排序的结 果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['Bob', 'Joe', 'Will'], dtype='<U4')"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'])\n",
    "np.unique(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ints = np.array([3, 3, 3, 2, 2, 1, 1, 4, 4])\n",
    "np.unique(ints)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True, False, False,  True,  True, False,  True])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "values = np.array([6, 0, 0, 3, 2, 5, 6])\n",
    "np.in1d(values, [2, 3, 6])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数组集合运算\n",
    "|方法|说明|\n",
    "|:-|:-|\n",
    "|unique(x)|计算x中的唯一元素，并返回有序结果|\n",
    "|intersect1d(x, y)|计算x和y中的公共元素，并返回有序结果|\n",
    "|union1d(x, y)|计算x和y的并集，并返回有序结果|\n",
    "|in1d(x, y)|得到一个表示“x的元素是否包含于y”的布尔型数组|\n",
    "|setdiff1d(x, y)|集合的差，即元素在x中且不在y中|\n",
    "|setxor1d(x, y)|集合的对称差，即存在于一个数组中但不同时存在于两个数组中的元素，异或|"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "3207008acf26a07c58f9163727980777c0d92fa298586eda8a314250b7931cb4"
  },
  "kernelspec": {
   "display_name": "Python 3.7.10 ('pybd')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
